﻿Module MetroAppRegistration
#Region "only metroapp reg"



    Private Function CreateMetroRegisterFile() As XDocument
        If System.IO.File.Exists(MyFile("regmetro.xml")) Then
            Return XDocument.Load(MyFile("regmetro.xml"))
        Else
            Dim xdoc As New XDocument
            Dim Root As XElement = <metroreg>

                                   </metroreg>
            xdoc.Add(Root)
            xdoc.Save(MyFile)
            Return xdoc
        End If
    End Function

    Function RegisterMetroStyleAppImpl(ByVal appid As String, ByVal sid As String, ByVal secret As String) As Boolean
        Dim successWrite = AddMetroRegElement(appid, sid, secret)
        Dim successRegister = False
        REM hier dann auf den pusher service zugreifen und die app mit den nötigen daten registrieren
        REM erst wenn beide seiten/functions true zurückgeben ist alles für metro registriert.
        If successRegister And successWrite Then
            Return True
        Else
            Return False
        End If
    End Function

    Function UnRegisterMetroStyleAppImpl(ByVal appid As String) As Boolean
        Dim successWrite = RemoveMetroRegFromXML(appid)
        Dim successPusher As Boolean = True
        REM dim successPusher = hier die rückgabe vom pushersvc 
        If successPusher And successWrite Then
            Return True
        Else
            Return False
        End If
    End Function


    Private Function AddMetroRegElement(ByVal appid As String, ByVal Sid As String, ByVal CSecret As String) As Boolean
        Dim xdoc = CreateMetroRegisterFile()

        Try
            Dim elements = From b In xdoc.Root.Elements
                           Where b.<appid>.Value = appid
                           Select b

            If elements(0) Is Nothing Then


                Dim newElement As XElement = <reg>
                                                 <appid><%= appid %></appid>
                                                 <sid><%= Sid %></sid>
                                                 <clsecret><%= CSecret %></clsecret>
                                                 <bearer></bearer>
                                             </reg>
                xdoc.Root.Add(newElement)
                xdoc.Save(MyFile)
            Else
                '            RemoveRegFromXML(appid)
                AddMetroRegElement(appid, Sid, CSecret)
            End If
            Return True
        Catch
            Return False
        End Try

    End Function


    Private Function RemoveMetroRegFromXML(ByVal tag As String) As Boolean
        Try
            Dim xdoc = CreateMetroRegisterFile()

            Dim user = From a In xdoc.Root.Elements
                       Where a.<appid>.Value = tag
                       Select a



            If user(0) IsNot Nothing Then
                user(0).Remove()
                xdoc.Save(MyFile)
            End If
            Return True
        Catch
            Return False
        End Try
    End Function

#End Region
End Module
